<template>
	<view class="login">
		<view class="login_box1"> 欢迎使用天气预警 </view>
		<view class="login_box2">
			<input class="login_box2_input" v-model="account" type="text" placeholder="请输入账号"
				@input="isButton(account, password)" />
		</view>
		<view class="login_box3">
			<input class="login_box2_input" v-model="password" password="true" placeholder="请输入密码"
				@input="isButton(account, password)" />
		</view>
		<view class="login_box5" @click="zhuce()">
			还没有账号，立即注册！
		</view>
		<view class="login_box4">
			<button class="btn" type="primary" @click="go()" :disabled="!isButtons">登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				password: '',
				account: '',
				isButtons: false
			}
		},
		onLoad() {
			// this.aaa()
		},
		methods: {
			zhuce() {
				uni.navigateTo({
					url: "../zhuce/zhuce",
				});
			},
			isButton(a, b) {
				if (a.length != 0 && b.length != 0) {
					this.isButtons = true;
					console.log(this.isButtons);
				}
				if (a.length == 0 || b.length == 0) {
					this.isButtons = false;
					console.log(this.isButtons);
				}
			},
			async go() {
				uni.showLoading({
					title: "登录中",
				});
				let {
					result
				} = await this.$store.state.user.where({
					userName: this.account
				}).get()
				console.log(result)
				if (result.data.length == 0) {
					uni.showToast({
						title: "用户名错误",
						duration: 2000,
						icon: "error",
					});
					return
				}
				if (this.account == result.data[0].userName) {
					if (this.password != result.data[0].passWord) {
						uni.showToast({
							title: "密码错误",
							duration: 2000,
							icon: "error",
						});
						return
					}
					if (this.password == result.data[0].passWord) {
						uni.showToast({
							title: "登录成功",
							duration: 2000,
							icon: "success",
						});
						setTimeout(() => {
							uni.switchTab({
								url: '../tabbar/tabbar-homepage/homepage'
							});
						}, 2000)

					}
				}
			},
			async aaa() {
				// promise方式
				uniCloud.callFunction({
						name: 'test',
						data: {
							a: 1
						}
					})
					.then(res => {
						console.log(res)
					});
				let res = await this.$store.state.user.get()
				console.log(res)
			}
		}
	}
</script>

<style>
	@import "../../common/css/index.css";
</style>
